Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to convert from a hex string to Color32 using macros #1596

Merged
merged 9 commits into from
May 15, 2022

Conversation

newcomb-luke
Copy link
Contributor

@newcomb-luke newcomb-luke commented May 7, 2022

This PR closes #1492.

Again currently ./sh/check.sh is currently failing for me, see my other PR.
Cargo clippy, cargo test, and cargo fmt have all been run.
The CHANGELOG.md has been updated under "Unreleased"

This change allows you to create Color32's from hex strings:

let color = epaint::hex_rgb!("#4c4c4c");

And it does so at compile-time.

@newcomb-luke newcomb-luke marked this pull request as draft May 7, 2022 16:51
@newcomb-luke newcomb-luke marked this pull request as ready for review May 7, 2022 16:52
@newcomb-luke newcomb-luke changed the title Draft: Add ability to convert from a hex string to Color32 using macros Add ability to convert from a hex string to Color32 using macros May 7, 2022
@emilk
Copy link
Owner

emilk commented May 11, 2022

Very nice!

But I would suggest we simplify it. As far as I can recall I've never seen premultiplied alpha expressed in hex form. So I suggest we boil it down to a single macro that accepts both RGB and unmultiplied RGBA (epaint::color_hex!("#55eedd") == epaint::color_hex!("#55eeddff")).

For additive, users can just do epaint::color_hex!("#ffeedd").additive() (we can make Color32::additive const fn)

@newcomb-luke
Copy link
Contributor Author

Upon further testing, previously the from_rgb, from_rgb_additive, from_rgba_premultiplied were const, however from_rgba_unmultiplied due to the floating-point conversions that have to take place, is unable to be const yet. So only the conversion from the string literal to the array of values can be const. So the macro invocation itself cannot yet be const, but soon it could be. This isn't much slower than using the regular from_rgba_unmultiplied though.

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a shame that it is not const but yeah, not much to do about that right now. I think this looks good, but the macro needs a docstring!

epaint/src/color.rs Show resolved Hide resolved
@newcomb-luke newcomb-luke requested a review from emilk May 15, 2022 00:31
@emilk emilk merged commit d2decfa into emilk:master May 15, 2022
@coderedart
Copy link
Contributor

is hex string assumed to be in linear space? or sRGB?
docs could mention that so its not confusing.
Color32 mention that Internally this uses 0-255 gamma space sRGBA color with premultiplied alpha. Alpha channel is in linear space..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Color32 from hex value
3 participants